package com.nopay.cdxphone.wx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.nopay.cdxphone.bean.response.WePayOrderResponse;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;

public class WXSDKManager {

    private static IWXAPI mApi;

    public static void init(Context context) {
        mApi = WXAPIFactory.createWXAPI(context, Constants.WX_APP_KEY, true);

        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mApi.registerApp(Constants.WX_APP_KEY);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
    }

    public static void share(Bitmap bitmap) {
        if(mApi == null) return;

        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
        bitmap.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        mApi.sendReq(req);
    }

    public static void shareApp(Context context) {
        String url = Constants.APK_PAGE_URL;
        String title = "畅电销自动拨号app下载";
        String description = "畅电销自动拨号app，是广州卡盟信息技术有限公司自主研发的产品，目前已服务全国各地电销行业的公司近千家，行业内好评如潮！app内置防骚扰过滤系统，管理系统，极大程度上提高了电销的工作效率。";
        share(context, url, title, description);
    }

    public static void share(Context context, String url, String title, String description) {
        if(mApi == null) return;

        WXWebpageObject webPage = new WXWebpageObject();
        webPage.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webPage);
        msg.title = title;
        msg.description = description;
        Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webPage");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        mApi.sendReq(req);
    }

    public static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    public static boolean sendPayReq(WePayOrderResponse.Data wePayReq) {
        PayReq request = new PayReq();
        request.appId = wePayReq.getAppid();
        request.partnerId = wePayReq.getPartnerid();
        request.prepayId= wePayReq.getPrepayid();
        request.packageValue = wePayReq.getPackageValue();
        request.nonceStr= wePayReq.getNoncestr();
        request.timeStamp= wePayReq.getTimestamp();
        request.sign= wePayReq.getSign();
        return mApi.sendReq(request);
    }
}
